package highTech.Thread.java5;

import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

public class CallableAndFuture {

	public static void main(String[] args) throws Exception {
//		ExecutorService threadPool=Executors.newSingleThreadExecutor();
//		Future<String> future=threadPool.submit(new Callable<String>() {
//			@Override
//			public String call() throws Exception {
//				Thread.sleep(2000);
//				return "hello";
//			}
//		});
//		System.out.println("�ȴ���");
//		System.out.println("�õ����:"+future.get());
//		threadPool.shutdown();
//		
		futureTest();
		
//		ExecutorService threadPool2=Executors.newFixedThreadPool(10);
//		CompletionService<Integer> completionService=new ExecutorCompletionService<Integer>(threadPool2);
//		for(int i=1;i<=10;i++){
//			final int seq=i;
//			completionService.submit(new Callable<Integer>() {
//				@Override
//				public Integer call() throws Exception {
//					Thread.sleep(new Random().nextInt(5000));
//					return seq;
//				}
//			});
//		}
//		
//		for(int i=1;i<=10;i++){
//			System.out.println(completionService.take().get());
//		}
//		threadPool2.shutdown();
	}
	
	private static void futureTest() throws InterruptedException, ExecutionException{
		FutureTask<String> future=new FutureTask<String>(new Callable<String>() {

			@Override
			public String call() throws Exception {
				Thread.sleep(2000);
				return "test";
			}
		});
		
		future.run();
//		new Thread(future).start();
		
		System.out.println(future.get());
		
	}
}
